Skip to content

Conversation

@simonresch
Copy link
Contributor

@simonresch simonresch commented Nov 5, 2025

Adds mutator support for generic classes.

Constructing mutators for generic classes would previously either fail or not work as expected due to parameter and return types being reported as java.lang.Object by the reflection API (type erasure). Since we can't provide a general Object mutator this would creation of a valid mutator for most generic classes.
Using the AnnotatedType of the class we resolve type parameters to create the correct underlying mutators.

Example fuzz test:

    public static class Pair<L, R> {
      L left = null;
      R right = null;

      public Pair(L left, R right) {
        this.left = left;
        this.right = right;
      }

      public L getLeft() {
        return this.left;
      }

      public R getRight() {
        return this.right;
      }
    }

  @FuzzTest
  void myFuzzTest(@NotNull(constraint = PropertyConstraint.RECURSIVE) Pair<String, Integer> data) {
    if (data.getLeft().equals("foobar") && data.getRight() == 1337) {
      throw new RuntimeException("Can fuzz generics");
    }
  }

@simonresch simonresch force-pushed the CIF-1866-support-generic-classes branch 3 times, most recently from f119aa8 to 581c4d5 Compare November 5, 2025 15:54
@simonresch simonresch changed the title feat: support generic constructor based beans feat: add mutator support for generic classes Nov 5, 2025
@simonresch simonresch marked this pull request as ready for review November 5, 2025 16:07
@simonresch simonresch requested review from a team and Copilot November 5, 2025 16:59
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for generic classes in the mutator framework by resolving type parameters at runtime. Previously, generic classes would fail to create valid mutators due to type erasure causing parameter and return types to be reported as java.lang.Object. The implementation uses AnnotatedType to resolve type parameters and construct mutators with the correct concrete types.

Key Changes:

  • Added type resolution logic to handle generic type parameters in constructors, methods, and return types
  • Updated all aggregate mutator factories to resolve generic types before creating mutators
  • Added comprehensive test coverage for generic classes across different mutator types (setter-based, constructor-based, cached, records)

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
BeanSupport.java Adds core type resolution methods for resolving generic type arguments in methods, constructors, and return types
AggregatesHelper.java Updates mutator creation to use resolved types instead of raw types from reflection API
CachedConstructorMutatorFactory.java Uses resolved parameter types when building mutators for generic classes
ConstructorBasedBeanMutatorFactory.java Resolves parameter types and return types when matching getters to constructors
SetterBasedBeanMutatorFactory.java Resolves setter parameter types to properly handle generic classes
SuperBuilderMutatorFactory.java Updates to use resolved parameter types for builder pattern support
SetterBasedBeanMutatorTest.java Adds test for generic class with setter-based mutation
RecordMutatorTest.java Adds test for generic record types
ConstructorBasedBeanMutatorTest.java Adds tests for generic classes with single and nested type parameters
CachedConstructorMutatorTest.java Adds test for generic class with cached constructor
StressTest.java Adds stress test cases for various generic class patterns

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@simonresch simonresch force-pushed the CIF-1866-support-generic-classes branch from 581c4d5 to 08f6331 Compare November 6, 2025 08:24
@simonresch simonresch force-pushed the CIF-1866-support-generic-classes branch from 08f6331 to a9ae722 Compare November 6, 2025 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants